/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao.Fornecedor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import pojo.Endereco;
import pojo.Fornecedor;
import util.SplitStringToNumberArray;

/**
 *
 * @author Andre Luiz
 */
public class FornecedorDaoImpl implements FornecedorDao{
    private final Collection<Fornecedor> fornecedores;
    
    public FornecedorDaoImpl(){
        this.fornecedores = new ArrayList<>();
    }
    
    @Override
    public ArrayList<Fornecedor> listar() {
       return (ArrayList<Fornecedor>) fornecedores; 
    }
    
    @Override
    public boolean remover(Fornecedor fornecedor) {
        return false;
    }

    @Override
    public Fornecedor buscarPorNome(Fornecedor fornecedor) {
        Fornecedor fornecedorTemp;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("smConfeccoesPU");
        EntityManager em = emf.createEntityManager ();
        try{
            em.getTransaction().begin();
            fornecedorTemp = em.find(fornecedor.getClass(),fornecedor.getId());
            em.getTransaction().commit();
            em.close();
            emf.close();
        }catch (IllegalArgumentException e) {
            return null;
        }
        return fornecedorTemp;
    }

    @Override
    public boolean salvar(Fornecedor fornecedor){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("smConfeccoesPU");
        EntityManager em = emf.createEntityManager ();
        try{
            em.getTransaction().begin();
            em.persist(fornecedor);
            em.getTransaction().commit();
            em.close();
            emf.close();
        }catch (RuntimeException e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean carregarDisco() {
        Fornecedor fornecedor;
        Endereco endereco;
        String itens;
        File arquivo = new File("registroFornecedores");
        if(!arquivo.exists()) return false;
        FileReader reader;
        BufferedReader leitor;
        try {
            reader = new FileReader(arquivo);
            leitor = new BufferedReader(reader);
            while(leitor.ready()){
                fornecedor = new Fornecedor();
                endereco = new Endereco();
                fornecedor.setNome(leitor.readLine());
                fornecedor.setId(Long.parseLong(leitor.readLine()));
                fornecedor.setTelefone(Long.parseLong(leitor.readLine()));
                endereco.setRua(leitor.readLine());
                endereco.setCep(Integer.parseInt(leitor.readLine()));
                endereco.setCidade(leitor.readLine());
                endereco.setComplemento(leitor.readLine());
                endereco.setNumero(Integer.parseInt(leitor.readLine()));
                endereco.setPais(leitor.readLine());
                fornecedor.setEndereco(endereco);
                itens = leitor.readLine();
                ArrayList<Long> split = SplitStringToNumberArray.convertLong(itens);
                fornecedor.setItens(split);
                this.salvar(fornecedor);
                leitor.readLine();
            }
            reader.close();
            leitor.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FornecedorDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {            
            Logger.getLogger(FornecedorDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
    
    @Override
    public boolean salvarDisco(){
        if (fornecedores == null) {
            return false;
        }
        try {
            FileWriter writer = new FileWriter("registroFornecedores");
            PrintWriter saida = new PrintWriter(writer);
            for (Fornecedor fornecedoresIterator : fornecedores) {
                saida.println(fornecedoresIterator.getNome());
                saida.println(fornecedoresIterator.getId());
                saida.println(fornecedoresIterator.getTelefone());
                saida.println(fornecedoresIterator.getEndereco().getRua());
                saida.println(fornecedoresIterator.getEndereco().getCep());
                saida.println(fornecedoresIterator.getEndereco().getCidade());
                saida.println(fornecedoresIterator.getEndereco().getComplemento());
                saida.println(fornecedoresIterator.getEndereco().getNumero());
                saida.println(fornecedoresIterator.getEndereco().getPais());
                if (fornecedoresIterator.getItens().isEmpty()) {
                    saida.print("#");
                } else {
                    for (long iteratorItens : fornecedoresIterator.getItens()) {
                        saida.print(iteratorItens);
                        saida.print('#');
                    }
                }
                saida.println("\n");
            }
            writer.close();
            saida.close();
        } catch (IOException e) {
            System.out.println(e.toString());
            return false;
        }
        return true;
    }
    
}